/**
 * 
 */
package de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.tests;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CSVTokenizer;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CreateFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.FeatureType;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.CaseFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.PunctuationFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.SuffixFeatures;
/**
 * @author Christoph Teichmann
 * created Jun 9, 2014 3:44:42 PM
 * @version 0.1
 */
public class TestCreateFeatures
{
	/**
	 * 
	 */
	private CreateFeatures cf;
	/**
	 *
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception
	{
		FeatureType[] ft = new FeatureType[] {new SuffixFeatures(2),new PunctuationFeatures(),
												new CaseFeatures()};
		cf = new CreateFeatures(ft);
	}
	/**
	 * Test method for {@link de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CreateFeatures#addFeatures(java.io.BufferedReader, de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.Tokenizer)}.
	 * @throws IOException 
	 */
	@Test
	public final void testAddFeatures() throws IOException
	{
		StringReader sr = new StringReader(TestStrings.ALIGNMENTS);
		BufferedReader br = new BufferedReader(sr);
		cf.addFeatures(br, new CSVTokenizer(false));
		br.close();
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		cf.writeOutFeatureLexicon(bw);
		bw.close();
		String[] parts = sw.toString().split("\\n");
		Set<String> s = new HashSet<>();
		for(String q : parts)
		{s.add(q);}
		System.out.println(s);
		assertTrue(s.contains("bt.SUFFIXFEATURES	SUFFIXFEATURES	multinomial"));
		sw = new StringWriter();
		bw = new BufferedWriter(sw);
		cf.writeOutFeatures(bw);
		bw.close();
		parts = sw.toString().split("\\n");
		s = new HashSet<>();
		for(String q : parts)
		{s.add(q);}
		assertTrue(s.contains(".|DEUTSCH	2.0	LOWERCASE.CASEFEATURE	2.0	ONLYPUNCT.PUNCTUATIONFEATURES	2.0	..SUFFIXFEATURES	2.0"));
		assertTrue(s.contains("har|DEUTSCH	2.0	LOWERCASE.CASEFEATURE	1.0	NOPUNCT.PUNCTUATIONFEATURES	2.0	INITIALUPPERCASE.CASEFEATURE	1.0	ar.SUFFIXFEATURES	2.0"));
		sw = new StringWriter();
		bw = new BufferedWriter(sw);
		cf.writeOutMapping(bw);
		bw.close();
		parts = sw.toString().split("\\n");
		s = new HashSet<>();
		for(String q : parts)
		{s.add(q);}
		assertTrue(s.contains("Kk|ENGLISH	kk|ENGLISH"));
	}
}